<template>
  <el-form
    :model="ruleForm"
    :rules="rules"
    ref="formRef"
    label-width="100px"
    class="demo-ruleForm"
  >
    <el-form-item label="用户账号" prop="username">
      <el-input v-model="ruleForm.username"></el-input>
    </el-form-item>
    <el-form-item label="默认密码" prop="password">
      <el-input v-model="ruleForm.password"></el-input>
    </el-form-item>
    <el-form-item label="关联角色" prop="roleId">
      <el-select v-model="ruleForm.roleId" placeholder="请选择角色" clearable>
        <el-option
          v-for="item in roleList"
          :key="item.objectId"
          :label="item.roleName"
          :value="item.objectId"
        >
        </el-option>
      </el-select>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="submitForm"> 新增账号 </el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { roleGet, userReg } from "@/api/user.js";
export default {
  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
        roleId: "",
      },
      rules: {
        username: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        roleId: [{ required: true, message: "请选择角色", trigger: "change" }],
      },
      roleList: [], //角色下拉列表数据
    };
  },
  created() {
    roleGet().then((res) => {
      this.roleList = res.data.results;
    });
  },
  methods: {
    submitForm(formName) {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          console.log(this.ruleForm);
          userReg(this.ruleForm);
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
